<template>
  <div class="app-container">
    <u-page title="函云传输">

      <u-filtered>

        <el-form :inline="true" :model="listQuery" class="search-form">
          <el-form-item label="订单号">
            <el-input v-model="listQuery.orderNo" placeholder="函云订单号" clearable />
          </el-form-item>
          <el-form-item label="商友">
            <el-input v-model="listQuery.orderNumber" placeholder="商友订单号" clearable />
          </el-form-item>
          <el-form-item label="收银台">
            <el-input v-model="listQuery.posid" placeholder="收银台编号" clearable style="width: 120px;" />
          </el-form-item>
          <el-form-item label="传输状态" class="ack">
            <el-select v-model="listQuery.ack" placeholder="传输状态" clearable>
              <el-option label="全部" value="" />
              <el-option label="成功" value="1" />
              <el-option label="失败" value="2" />
              <el-option label="待传输" value="0" />
            </el-select>
          </el-form-item>
          <el-form-item label="传输时间">
            <u-date-time-picker v-model="searchDate" @change="changeSearchDate" />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="hdlFilter">搜索</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-refresh-right" @click="hdlRetry">一键重传</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-refresh-right" @click="hdlRetry2">一键重传2</el-button>
          </el-form-item>
        </el-form>

      </u-filtered>

      <u-table
        :data="list"
        :loading="listLoading"
        :options="listOptions"
        :columns="columns"
        :pagination.sync="listQuery"
        :fetch="hdlList"
      >
        <template v-slot:right>
          <el-table-column label="操作" align="center" fixed="right">
            <template slot-scope="{ row }">
              <el-button type="text" @click="hdlRetryById(row)">重传</el-button>
              <el-button type="text" @click="handleOrderDetail(row)">交易记录</el-button>
              <el-button type="text" @click="orderDetailToJson(row)">传输内容</el-button>
            </template>
          </el-table-column>
        </template>
      </u-table>

    </u-page>

    <u-dialog title="交易记录" :show.sync="dialogVisible" :footer="false" width="45%">
      <el-form ref="dialogForm" :model="jyData" label-position="top" label-width="80px">

        <el-form-item label="订单信息">
          <el-descriptions :column="1" border>
            <el-descriptions-item label="订单号">{{ jyData.uuid }}</el-descriptions-item>
            <el-descriptions-item label="商友订单号">{{ jyData.orderNumber }}</el-descriptions-item>
            <el-descriptions-item label="交易时间">{{ jyData.tradingTime }}</el-descriptions-item>
            <el-descriptions-item label="记账日期">{{ jyData.tradingDate }}</el-descriptions-item>
            <el-descriptions-item label="销售金额">{{ jyData.totalAmount }}</el-descriptions-item>
            <el-descriptions-item label="实收金额">{{ jyData.salesAmount }}</el-descriptions-item>
            <el-descriptions-item label="优惠金额">{{ jyData.discAmount }}</el-descriptions-item>
            <el-descriptions-item label="会员卡号">{{ jyData.vipCard }}</el-descriptions-item>
            <el-descriptions-item label="收银员">{{ jyData.cashierName }}</el-descriptions-item>
          </el-descriptions>
        </el-form-item>

        <el-form-item label="商品列表">
          <el-table :data="jyData.goodsList" style="width:fit-content" border>
            <el-table-column prop="goodsName" label="商品名称" width="130" align="center" show-overflow-tooltip />
            <el-table-column prop="goodsId" label="商品代码" width="130" align="center" show-overflow-tooltip />
            <el-table-column prop="unitPrice" label="收款金额" width="130" align="center" show-overflow-tooltip />
            <el-table-column prop="salesVolumes" label="销售数量" width="130" align="center" show-overflow-tooltip />
          </el-table>
        </el-form-item>

        <el-form-item label="支付列表">
          <el-table :data="jyData.payList" style="width:fit-content;margin-top: 1%;" border>
            <el-table-column prop="payCode" label="收款代码" width="130" align="center" show-overflow-tooltip />
            <el-table-column prop="payName" label="交易款别" width="130" align="center" show-overflow-tooltip />
            <el-table-column prop="payValue" label="收款金额" width="130" align="center" show-overflow-tooltip />
          </el-table>
        </el-form-item>

      </el-form>
    </u-dialog>

    <u-dialog title="传输内容" :show.sync="jsonDialogVisible" :footer="false" width="45%" class="code">
      <el-input v-model="detailJson" autosize type="textarea" placeholder="{}" />
    </u-dialog>
  </div>
</template>
<script>

export default {
  data: function() {
    return {
      api: this.$u.api.PluginsAiaya,
      // 表格
      columns: [
        {
          prop: 'orderNo',
          label: '订单号'
        },
        {
          prop: 'orderNumber',
          label: '商友订单号'
        },
        {
          prop: 'posid',
          label: '收银台',
          width: '60'
        },
        {
          prop: 'ack',
          label: '传输状态',
          render: (h, params) => {
            if (params.row.ack === 1) {
              return h('el-tag', { props: { type: 'success' }}, '成功')
            } else if (params.row.ack === 2) {
              return h('el-tag', { props: { type: 'danger' }}, '失败')
            } else {
              return h('el-tag', { props: { type: 'warning' }}, '待传输')
            }
          }
        },
        {
          prop: 'createdAt',
          label: '传输时间',
          timestamp: true
        },
        {
          prop: 'updatedAt',
          label: '上传时间',
          timestamp: true
        },
        {
          prop: 'note',
          label: '备注'
        }
      ],

      list: [],
      listLoading: true,
      listOptions: {
        index: true,
        stripe: true // boolean 斑马纹
      },
      listQuery: {
        pageNumber: 1,
        pageSize: 10,
        totalCount: 1,
        beginTime: '',
        endTime: '',
        orderNumber: '',
        posid: '',
        ack: ''
      },
      dialogFormVisible: false,
      dataForm: {},
      jsonDialogVisible: false,
      dialogVisible: false,
      detailJson: {},
      jyData: {}
    }
  },
  created() {
    this.hdlList()
  },
  methods: {
    // 重试
    hdlRetryById(row) {
      const { id } = row
      const self = this
      self.$confirm('确定进行重传?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        self.api.fetchByRetryId(id).then(() => {
          // 刷新数据列表
          self.hdlList()
          // 通知信息
          self.$u.msg('操作成功')
        })
      }).catch(() => {
      })
    },
    // 查看传输内容
    orderDetailToJson(row) {
      const content = JSON.parse(row.content)
      this.detailJson = JSON.stringify(content, null, '\t')
      this.jsonDialogVisible = true
    },
    // 查询交易记录
    handleOrderDetail(row) {
      this.jyData = JSON.parse(row.content)
      this.dialogVisible = true
    },
    // 一键重试
    hdlRetry() {
      const self = this
      self.$confirm('确定进行一键重传?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        self.api.retry().then(() => {
          // 刷新数据列表
          self.hdlList()
          self.$u.msg('操作成功,请稍后查看')
        })
      }).catch(() => {
      })
    },
    // 一键重试
    hdlRetry2() {
      const self = this
      self.$confirm('确定进行一键重传?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        self.api.retry2().then(() => {
          // 刷新数据列表
          self.hdlList()
          self.$u.msg('操作成功,请稍后查看')
        })
      }).catch(() => {
      })
    }
  }
}
</script>

<style lang="scss" scoped>

::v-deep{
    .code .el-textarea .el-textarea__inner {
        background-color: #060606 !important;
        color: #c6c6c6 !important;
    }

    .ack .el-input{
      width: 120px;
    }
}

</style>
